<!DOCTYPE html>
<html>
    <head>
        <title>
            猜数字游戏
        </title>
    </head>
    <body>
        <h1>猜数字游戏</h1>
        <button onclick="random_num()">开始</button><br><br><br>
        <p id="a"></p>
        输入你猜的数:<input type="text" id="num">
        <button onclick="guess_num()">check!</button>
        <p id="xs">线索</p>
        <p id="demo">result</p>
        <script>
            var temp = 5;
            var num=[0,0,0,0];
            function random_num()
            {
                var aa=0,bb=0,tt;
                num[0]=Math.floor(Math.random() * 10);
                for(var i=0;i<4;i++)
                {   
                    while(1)
                    {
                        tt=Math.floor(Math.random() * 10);
                        for(var j=0;j<i;j++)
                        {
                            if(num[j]==tt)
                            {
                                tt=0;
                                break;
                            }
                        }
                        if(tt!=0)
                        {
                            num[i]=tt;
                            break;
                        }
                    }     
                }
            }
            function guess_num()
            {
                
                var xx = "你还有"+temp+"次机会";
                temp--;
                document.getElementById("a").innerHTML = xx;
                if(temp<0)
                {
                    document.getElementById("demo").innerHTML = num;
                    return 0;
                }
                var txt =" ";
                var b;
                var aa=0,bb=0;
                var t=0; 
                var a = document.getElementById("num").value;
                b = a.split("");
                
                 for(var i=0;i<4;i++)
                {
                    for(var j=0;j<4;j++)
                    {
                        if(num[i]==b[j])
                        {
                            
                            if(i==j)
                            {
                                aa++;
                            }
                            else bb++;                           
                        }
                    }
                }
                
                var x=aa+"A"+"   "+bb+"B";
                document.getElementById("xs").innerHTML = x;
                if(aa==4)
                    {
                        document.getElementById("demo").innerHTML = "你可太棒了！";
                    }               
                if(t==0)
                {
                    text += num[i] + " ";
                    document.getElementById("demo").innerHTML = text;
                }
            }
        </script>
    </body>
</html>